package com.apps.sdses.flink141.demo;

import com.google.gson.Gson;
import org.apache.flink.table.functions.FunctionContext;
import org.apache.flink.table.functions.ScalarFunction;
import org.apache.flink.types.Row;

import java.util.HashMap;
import java.util.Map;

/**
 * author: codejiwei
 * date: 2023/8/24
 * desc:
 **/
public class RowToJsonUDF extends ScalarFunction {
    Gson gson = null;
    @Override
    public void open(FunctionContext context) throws Exception {
        gson = new Gson();
        super.open(context);
    }
    public String eval(Row row) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", row.getField(0));
        map.put("name", row.getField(1));
        map.put("age", row.getField(2));
        map.put("gender", row.getField(3));
        return gson.toJson(map);

    }

}
